package com.webull.information.center.carwler.common.util.news.detection;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.NewsDetection;
import com.webull.information.center.carwler.common.model.NewsDetection.DetectionResult;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

@Component
public class DetectionYahooList implements DetectionList {
	private final Logger logger = LogManager.getLogger(getClass());
	private @Autowired
    HttpJsoupClient htmlPrase;

	private static final String R_URL = "http://finance.yahoo.com/quote/";

	// String url2 = "http://finance.yahoo.com/quote/AAPL";
	// url2 =
	// "http://www.reuters.com/finance/stocks/companyNews?symbol=600036.SS";
	@Override
	public List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker) {
		String url = R_URL + ticker.getsSymbol();
		if (StringUtils.isNotBlank(ticker.getsExchange()))
			url += "." + ticker.getsExchange();
		Document doc = htmlPrase.parseDocumentFromUrl(url, true);
		// url = "http://finance.yahoo.com/quote/000001.SS";
		// Connection connection = Jsoup.connect(url)
		// .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
		// (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
		// // .header("x-client-data",
		// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
		// .header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		//
		// connection.proxy("127.0.0.1", 1080);
		//
		// Document doc = null;
		// try {
		// doc = connection.timeout(10000).get();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		if (doc == null) {
			logger.warn("DetectionYahooList parseNewsList parse url:{} ,doc is null!!", url);
			return null;
		}

		return parseNewsList(ticker, doc);
	}

	private List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker, Document doc) {
		List<NewsDetection.DetectionResult> result = new ArrayList<>();

		// 美股新闻页面
		Optional.ofNullable(doc.select("div#quoteNewsStream-0-Stream>ul>li"))//
				.ifPresent(v -> v.forEach(v0 -> {
					NewsDetection.DetectionResult item = new NewsDetection.DetectionResult(ticker);
					item.setUrl(Optional.ofNullable(v0.select("h3>a").first()).map(a -> a.absUrl("href")).orElse(null));
					if (!StringUtils.isBlank(item.getUrl()))
						item.setUrlMd5(HttpJsoupClient.finishingMd5Url(item.getUrl() + item.getsSource()));
					item.setTitle(Optional.ofNullable(v0.select("h3").first()).map(h -> h.text()).orElse(null));
					item.setNewsTime(DateUtils.addHours(new Date(), -1));
					result.add(item);
				}));

		return result;
	}

	public static void main(String[] args) {
		DetectionYahooList ll = new DetectionYahooList();
		NewsDetection.Detection ticker = new NewsDetection.Detection();
		ticker.setTickerId(129384);
		ticker.setsExchange("SS");
		ticker.setsSource("ABC");
		ticker.setsSymbol("000001");
		List<DetectionResult> a = ll.parseNewsList(ticker);
		for (int i = 0; i < a.size(); i++) {
			System.out.println(a.get(i).getTitle());
		}
		System.out.println(a);
	}
}
